package cc.rengu.igas.bsps.common.dao.impl;

import cc.rengu.igas.bsps.common.constant.BspsTreeNodeConstant;
import cc.rengu.igas.bsps.common.dao.BspSequenceMapper;
import cc.rengu.oltp.service.common.constant.AppConfigConstant;
import cc.rengu.oltp.service.common.constant.AppParamConstant;
import cc.rengu.oltp.service.common.entity.SequenceInfo;
import cc.rengu.oltp.utility.util.DbsUtil;
import cc.rengu.oltp.utility.util.XmlConfigUtil;
import cc.rengu.oltp.utility.util.XmlTreeUtil;
import cc.rengu.utility.cache.UnifiedCache;
import cc.rengu.utility.log.RgLog;
import cc.rengu.utility.log.RgLogger;

/**
 * 商户服务Sequence实现类
 * Created by 王成 on 2018/5/29.
 */
public class BspSequenceMapperImpl implements BspSequenceMapper {
    private RgLogger rglog = RgLog.getLogger(this.getClass().getName());

    @Override
    public String getNextEventSeqNum(int length) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.OLTP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.OLTP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "getNextEventSeqNum");
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, null, null);
        if (0 != iReturnCode) {
            rglog.error("获取事件申报SEQUENCE序号失败,iReturnCode:<{}>", iReturnCode);
            return null;
        }
        /* 不足长度前补0 */
        String eventSeqNum = xmlTreeUtil.getXmlTreeStringValue(BspsTreeNodeConstant.EVENT_SEQ_NUM);
        if (eventSeqNum.length() < length) {
            String format = "%0" + (length - eventSeqNum.length()) + "d";
            return String.format(format, 0) + eventSeqNum;
        } else {
            return eventSeqNum;
        }
    }

    @Override
    public String getNextMchntApplyId(int length) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "getNextMchntApplyId");
        SequenceInfo sequenceInfo = new SequenceInfo();
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, null, sequenceInfo);
        if (0 != iReturnCode) {
            rglog.error("获取商户申请单号失败,iReturnCode:<{}>", iReturnCode);
            return null;
        }
        /* 不足长度前补0 */
        String mchntApplyId = sequenceInfo.getSequenceNo();
        ;
        if (mchntApplyId.length() < length) {
            String format = "%0" + (length - mchntApplyId.length()) + "d";
            return String.format(format, 0) + mchntApplyId;
        } else {
            return mchntApplyId;
        }
    }

    @Override
    public String getNextQrCodeSeq(int length) throws Exception {
        XmlTreeUtil xmlTreeUtil = new XmlTreeUtil();
        String dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(AppParamConstant.DATABASE_CONF_NODE, AppConfigConstant.MCMP_POOL_NAME);
        String corporation = xmlTreeUtil.getXmlTreeStringValue(AppParamConstant.CORPORATION);
        if (null != corporation && !corporation.isEmpty()) {
            String dbPoolCashNode = corporation.trim() + AppParamConstant.DATABASE_CONF_NODE;
            dbPoolName = XmlConfigUtil.getXmlConfigCacheKeyValue(dbPoolCashNode, AppConfigConstant.MCMP_POOL_NAME);
        }
        DbsUtil dbsUtil = new DbsUtil(dbPoolName);
        String sqlStr = (String) UnifiedCache.get(AppParamConstant.SQL_MAPPER_CACHE, "getBspsNextQrCodeSeq");
        SequenceInfo sequenceInfo = new SequenceInfo();
        int iReturnCode = dbsUtil.dbsExecuteSql(sqlStr, null, sequenceInfo);
        if (0 != iReturnCode) {
            rglog.error("获取二维码序号失败,iReturnCode:<{}>", iReturnCode);
            return null;
        }
        /* 不足8位长度前补0 */
        String qrCodeSeqNum = sequenceInfo.getSequenceNo();
        if (qrCodeSeqNum.length() < length) {
            String format = "%0" + (length - qrCodeSeqNum.length()) + "d";
            return String.format(format, 0) + qrCodeSeqNum;
        } else {
            return qrCodeSeqNum;
        }
    }

}
